import { Http, RequestConfig, Request } from 'butterfly-decorators'

// 请求拦截器
const reqInterceptorFn = (req: RequestConfig) => {
  return req
}
// 响应拦截器
const resInterceptorFn = async (response: Response) => {
  try {
    const res = await response.json()
    console.log(res)
    // if (res.code != 200) {
    //   window.$msg.error(res.message || '未知错误请联系管理员')
    // }
    return res
  } catch (error) {
    console.log(error)
    return response
  }
}

@Http({
  baseUrl: import.meta.env.BUTTERFLY_BASE_API_URL,
  interceptors: {
    req: reqInterceptorFn,
    res: resInterceptorFn
  }
})
export class HttpReq {}

export const registerHttp = () => {
  window.$req = new Request({ baseUrl: import.meta.env.BUTTERFLY_BASE_API_URL })
  window.$req.interceptors.request.use(reqInterceptorFn)
  window.$req.interceptors.response.use(resInterceptorFn)
}
